<%@ page contentType="text/html;charset=UTF-8"%>
<script type="text/javascript" src="${ctxFront }/js/PCASClass.js"></script>

<script type="text/javascript"> 
	function iEventSetHeaderTitle(){
		$("#main_footer").hide();
		$("#addr_footer").show();	
		iEventGoBack = function(){
			jumpPage('#mainPage');
			document.location="iEvent://setNavgitionInfo?fun=iEventGetNavgitionInfo()";
		}
		var result = {
			title:"收货信息",
		    leftBtn:{ 
		    	type:0,	//0只显示返回,1只显示关闭，2显示返回，关闭
				backFun:"iEventGoBack()"   //返回执行的事件，不带的话由iOS控制。
			}              
		};
		if($global_isAndroid){
			window.control.onJsComplete(JSON.stringify(result));
			return;
		}
		return JSON.stringify(result);
	}
	function iEventSetToSettlement(){
		var result = {
			title:"订单详情",
		    leftBtn:{ 
		    	type:2,	//0只显示返回,1只显示关闭，2显示返回，关闭
				backFun:""   //返回执行的事件，不带的话由iOS控制。
			}
			//,
			//rightBtn:{
			//	bottons:[
			//		{
			//			type:0,			///0就是图片，1是文字,
			//			name:"webNavMore.png",		//更多webNavMore.png 搜索webNavSearch.png
			//			fun:"iEventShowMore()"
			//		}
			//	]
			//}               
		};
		return JSON.stringify(result);
	}
</script>
<script type="text/javascript">
	$(document).ready(function() {
		//jumpPage('#addAddrPage');
	});
	function saveAddress(){
		//验证是否登录
		if(!checkLogin()) return;
		
		if(trim($("#name").val()).length==0){
			$("#messageBox").text("请输入收货人姓名.");
			$("#messageBox").show();
			$("#messageBox").animate({opacity: "hide"}, 3000);
			$("#name").focus();
			return;
		}
		if(trim($("#phone").val()).length==0){
			$("#messageBox").text("请输入联系号码.");
			$("#messageBox").show();
			$("#messageBox").animate({opacity: "hide"}, 3000);
			$("#phone").focus();
			return;
		}
		//if($("#phone").val()!=""){
		//	var value = $("#phone").val();
		//	var length = value.length;
    		//if(length != 11 || !/^1[2,3,4,5,7,8]\d{9}$/.test(value)){
    	//	if(length != 11 && length != 8){
    	//		$("#messageBox").text("请输入正确的联系电话.");
		//		$("#messageBox").show();
		//		$("#messageBox").animate({opacity: "hide"}, 3000);
		//		$("#phone").select();
		//		return;
    	//	}
		//}
		if(trim($("#userAddr").val()).length==0){
			$("#messageBox").text("请选择所属区县.");
			$("#messageBox").show();
			$("#messageBox").animate({opacity: "hide"}, 3000);
			$("#userAddr").focus();
			return;
		}
		if(trim($("#address").val()).length==0){
			$("#messageBox").text("请输入详细地址.");
			$("#messageBox").show();
			$("#messageBox").animate({opacity: "hide"}, 3000);
			$("#address").focus();
			return;
		}
		
		//开始执行提交保存
		var param = "id="+$("#addressId").val()+"&user.id="+$("#userId").val()+"&name="+$("#name").val()+"&phone="+$("#phone").val()+"&province="+$("#province").val()+"&city="+$("#city").val()+"&county="+$("#county").val()+"&address="+$("#address").val()+"&defaultFlag=0"+"&remarks=";
		loading('正在提交，请稍等...');
		//异步获取更多
		$.ajax({
			type: 'post',
			url: '${ctxShop}/address/save/orderAddress',
			data: param,
			dataType: 'json',
			success: function(data){
				hideLoader();
				if(data.status==200){
					var result = data.result;
					$(".address-option").remove();
					for(var i=0; i<result.length; i++){
						var address = result[i];
						var html = "";
						html+='<li class="address-option" id="'+address.id+'" data-icon="false">';
					    html+='<a href="#" class="address" onclick="choiceAddress('+(i+1)+',\''+address.county+'\')" style="';
					    html+=address.defaultFlag==1?'background: rgb(241, 241, 241);">':'background: rgb(255, 255, 255);">';
				    	html+='<label class="float-left choice-address-detail" style="margin: 0em;">';
				    	html+=address.name+'&nbsp;&nbsp;'+address.phone+'<br/>';
				    	html+=address.detailAddress;
				    	html+='</label>';
				    	html+='<label class="float-right" style="margin: 5px 0px 0px 0px;">';
				    	if(i==(result.length-1)){
						html+='<img src="${ctxFront }/images/shop/radio-icon1.png" id="address'+(i+1)+'" alt="" onclick="choiceAddress('+(i+1)+',\''+address.county+'\')" width="20" height="20" style="margin-top: 5px;">';						    				
				    	}else{
				    	html+='<img src="${ctxFront }/images/shop/radio-icon2.png" id="address'+(i+1)+'" alt="" onclick="choiceAddress('+(i+1)+',\''+address.county+'\')" width="20" height="20" style="margin-top: 5px;">';
				    	}
				    	html+='</label>';
				    	if(address.defaultFlag==1){
				    	html+='<label class="float-right" style="margin: 5px 1em 0px 0px; color: red;">';
				    	html+='(默认地址)';
				    	html+='</label>';
				    	}
					    html+='</a>';
						html+='</li>';
						$("ul.address-choice").append(html);
						if(i==(result.length-1)){
							$(".formAddress").show();
							$(".startAddress").hide();
							var addressDetail=address.name+'&nbsp;&nbsp;'+address.phone+'<br/>';
				    		addressDetail+=address.detailAddress;
				    		$("input[id='userAddress']").val(address.id);
							$("#address-detail").html(addressDetail);
						}
					}
					$("ul.address-choice").listview('refresh'); // 刷新listview控件
					$("#address").val("");
					$("#userAddr").val("");
					openPopup("保存成功");
					//初始化页眉页脚
					$("#main_footer").show();
					$("#addr_footer").hide();
					closeAddressDiv();
					//document.location.href=localUrl+"#mainPage";
					jumpPage('#mainPage');
					document.location="iEvent://setNavgitionInfo?fun=iEventGetNavgitionInfo()";
					addressLen++;
					//手机端app请求协议
					//document.location="iEvent://setNavgitionInfo?fun=iEventSetToSettlement()";
					$area=$("#county").val();
					refreshReceiptTime();
				}else{
					$("#messageBox").text(data.message);
					$("#messageBox").show();
					$("#messageBox").animate({opacity: "hide"}, 3000);
				}
			},
			failure: function(){
				$("#messageBox").text("操作失败");
				$("#messageBox").show();
				$("#messageBox").animate({opacity: "hide"}, 3000);
			}				
		});
	}
	var $province = "";
	var $city = "";
	var $county = "";
	//跳到上海市的地址选择页面
	function toSHAddrPage(obj){
		$("#addr_footer").hide();
		$("#add_address").empty();//先清空记录
		var proHtml="";
		for(var i=1;i<PCAA[0][0].length;i++){
			proHtml += '<li><a href="#" class="ui-btn" onclick="backAddres(this)" data-transition="flip">'+PCAA[0][0][i]+'</a></li>';
		}
		$("#add_address").append(proHtml);
		document.location="iEvent://setNavgitionInfo?fun=iEventSetHeaderTitle()";
		document.location.href=localUrl+"#checkProPage";
	}
	//选择【区/县】后返回收货地址页面
	function backAddres(obj){
		$("#addr_footer").show();
		$county=$(obj).html();
		$province='上海市';
		$city='上海市';
		$("input[id='userAddr']").val($city+$county);
		$("input[name='province']").val($province);
		$("input[name='city']").val($city);
		$("input[name='county']").val($county);
		jumpPage("#addAddrPage");
		//document.location.href=localUrl+"#addAddrPage";
	}
</script>
<div data-role="page" id="addAddrPage">
	<div role="main" class="ui-content"  style="padding:0px;">
		<div id="popup" data-role="popup" data-position-to="window" data-overlay-theme="a" data-theme="b" class="ui-content">
		  <p></p>
		</div>
		<form:form id="inputForm" modelAttribute="address" action="${ctxShop}/address/save" method="post" class="form-horizontal">
			<form:hidden id="addressId" path="id"/>
			<form:hidden id="userId" path="user.id"/>
			<tags:mbmessage content="${message}"/>
			<c:if test="${receiptTime.remarks!='' && receiptTime.remarks!=null }">
			<div class="fieldcontain address-remarks">
				收货提示：${receiptTime.remarks }
			</div>
			</c:if>
			<div class="fieldcontain address-div">
				<form:input path="name" htmlEscape="false" maxlength="20" class="required address-info2" data-role="none" placeholder="收货人姓名" />
			</div>
			<div class="fieldcontain address-div">
				<form:input path="phone" htmlEscape="false" maxlength="12" class="required address-info2" data-role="none" placeholder="手机号码" />
			</div>
			<div class="fieldcontain address-div">
				<input id="userAddr" value="" onclick="toSHAddrPage(this)" maxlength="100" class="required address-info2" data-role="none" placeholder="所属区县" />
				<form:hidden path="province"/>
				<form:hidden path="city"/>
				<form:hidden path="county"/>
			</div>
			<div class="fieldcontain address-div">
				<form:input path="address" htmlEscape="false" maxlength="200" class="required address-info2" data-role="none" placeholder="详细地址" />
			</div>
		</form:form>
	</div>
	<div data-role="footer" class="ui-footer" id="addr_footer"  style="background-color:white;border-color:#DDD;">
		<div class="footer-right" style="left: 50%; margin-left: -45.5px;">
			<img alt="" src="${ctxFront }/images/shop/saveSubmit.png" height="35" style="margin: 7.5px 0em 7.5px 0px;" onclick="saveAddress()">
	    </div>
	</div><!-- /footer -->
</div>
<!-- 只有上海市的地址 -->
<div data-role="page" id="checkProPage">
	<!-- div data-role="header" data-position="fixed" data-tap-toggle="false">
		<div data-role="controlgroup" data-type="horizontal" class="ui-btn-left">
	    	<a href="#" onclick="jumpPage('#addAddrPage')"><img class="head-back-icon" src="${ctxFront}/images/head/android/back.png"/></a>
	    </div>
		<h1>上海市</h1>
	</div -->
	<div data-role="main" style="padding:0em;">
	    <ul data-role="listview" id="add_address">
	    </ul>
	</div>
</div> 
